package com.mach.platform.service;

import com.mach.platform.domain.CtrlProcess;
import com.mach.platform.domain.EnergyStruc;
import com.mach.platform.repository.CtrlProcessRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.service
 * @time 2018/11/16 0016 15:57
 */
@Service
public class CtrlProcessServiceImpl implements BaseService<CtrlProcess> {

	@Autowired
	private CtrlProcessRepo ctrlProcessRepo;

	@PersistenceContext
	EntityManager em;
	/**
	 * 保存
	 *
	 * @param ctrlProcess 保存的实体
	 * @return T 返回保存后的实体
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public CtrlProcess save (CtrlProcess ctrlProcess) {
		return null;
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public List<CtrlProcess> insertObjInBatch (List<CtrlProcess> list) {
		return null;
	}

	/**
	 * 更新
	 *
	 * @param ctrlProcess 跟新的实体
	 * @return T
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public CtrlProcess update (CtrlProcess ctrlProcess) {
		return null;
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public int delete (Long[] ids) {
		return 0;
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<CtrlProcess> list (Pageable pageable, Map params) {
		Specification condications = DynamicSpecifications.bySearchFilter(SearchFilter.parse(params).values(), CtrlProcess.class);
		return ctrlProcessRepo.findAll(condications, pageable);
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public CtrlProcess info (Long id) {
		return null;
	}

	/**
	 * 保存运行次数
	 * @param devId
	 * @param times
	 * @return
	 */
	@Transactional(rollbackOn = Exception.class)
	public void saveLaunchTimes (Long devId, Integer times) {

		String sql = "replace into iot_dev_prop(name, value, dev_id) value ('launchRecord',"+times+","+devId+")";
		int executeUpdate = em.createNativeQuery(sql).executeUpdate();

	}

	@Transactional(rollbackOn = Exception.class)
	public void saveWarningDataInBatch (List<Map> maps) {

		StringBuilder sb = new StringBuilder("replace into iot_dev_prop(name, value, dev_id) values ");
		for (int i = 0; i < maps.size(); i++) {
			Map curMap = maps.get(i);
			sb.append("('" + curMap.get("name").toString() + "','" + curMap.get("value").toString() + "'," + curMap.get("devId") + ")");
			if (i < maps.size() - 1) {
				sb.append(",");
			}
		}
		int executeUpdate = em.createNativeQuery(sb.toString()).executeUpdate();

	}

}
